package com.ce.sys.role.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ce.framework.constants.WebConstants;
import com.ce.framework.datamaintain.dao.IBaseDao;
import com.ce.framework.datamaintain.implement.BaseLogic;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.StringUtil;
import com.ce.sys.role.dao.po.SysAccountRole;
import com.ce.sys.role.dao.po.SysRole;
import com.ce.sys.role.dao.po.SysRolePermiss;

public class RoleLogic extends BaseLogic implements IRoleLogic {

	private IBaseDao basedao;
	
	public IBaseDao getBasedao() {
		return basedao;
	}

	public void setBasedao(IBaseDao basedao) {
		this.basedao = basedao;
	}

	/* (non-Javadoc)
	 * @see com.csmis.sys.role.logic.IRoleLogic#addRole(com.csmis.sys.role.dao.po.SysRole, java.lang.String[], java.util.HashMap)
	 */
	public void addRole(SysRole role, String[] menuids, HashMap<?, ?> opts)
			throws AppException {
		// TODO Auto-generated method stub
        this.basedao.addPo(role);
        for(int i=0;i<menuids.length;i++){
        	SysRolePermiss permiss = new SysRolePermiss();
        	permiss.setVcId(GeneratorUUID.generateUUID());
        	permiss.setVcMenuId(menuids[i]);
        	if(opts.get(menuids[i])!=null)//可能存在没有操作功能的菜单，需要进行判断
        	    permiss.setVcOperation(opts.get(menuids[i]).toString());
        	permiss.setVcRoleId(role.getVcId());
        	this.basedao.addPo(permiss);
        }
	}

	/* (non-Javadoc)
	 * @see com.csmis.sys.role.logic.IRoleLogic#deleteRole(java.lang.String[])
	 */
	public void deleteRole(String[] delIds) throws AppException {
		// TODO Auto-generated method stub
		String ids = StringUtil.fromArrayToStr(delIds, false);
		if(ids.indexOf("admin")>=0)
			throw new AppException("删除的角色中包含系统管理员角色,该角色不能删除！");
		if(ids.indexOf("common")>=0)
			throw new AppException("删除的角色中包含通用角色,该角色不能删除！");
	   try{
			this.basedao.deleteLogic(SysRole.class.getName(), delIds, WebConstants.DEFAULT_DELETE_FIELD,null);//删除角色
	        this.basedao.deleteSearchHQL("from SysRolePermiss po where po.vcRoleId in ("+StringUtil.fromArrayToStr(delIds, true)+")"); //删除角色对应的菜单	
		}catch(Exception e){
			throw new AppException("删除角色失败！");
		}
	}

	/* (non-Javadoc)
	 * @see com.csmis.sys.role.logic.IRoleLogic#updateRole(com.csmis.sys.role.dao.po.SysRole, java.lang.String[], java.util.HashMap)
	 */
	public void updateRole(SysRole role, String[] menuids, HashMap<?, ?> opts)
			throws AppException {
		// TODO Auto-generated method stub
        this.basedao.updatePo(role);
        this.basedao.deleteSearchHQL("from SysRolePermiss po where po.vcRoleId = '"+role.getVcId()+"'"); //删除角色对应的所有菜单	
        for(int i=0;i<menuids.length;i++){
        	SysRolePermiss permiss = new SysRolePermiss();
        	permiss.setVcId(GeneratorUUID.generateUUID());
        	permiss.setVcMenuId(menuids[i]);
        	if(opts.get(menuids[i])!=null)//可能存在没有操作功能的菜单，需要进行判断
        	    permiss.setVcOperation(opts.get(menuids[i]).toString());
        	permiss.setVcRoleId(role.getVcId());
        	this.basedao.addPo(permiss);
        }
	}

	/* (non-Javadoc)
	 * @see com.csmis.sys.role.logic.IRoleLogic#addRelation(java.lang.String, java.lang.String)
	 */
	public void addRelation(String roleId, String userIds)throws AppException {
		// TODO Auto-generated method stub
		String[] ids = userIds.split(",");
		List<SysAccountRole> roleAccountList = new ArrayList<SysAccountRole>();
		this.basedao.deleteSearchHQL("from SysAccountRole po where po.vcRoleId = '"+roleId+"'");
		for(int i=0;i<ids.length;i++){
			if(StringUtil.isNotBlank(ids[i])){
				SysAccountRole role = new SysAccountRole();
				role.setVcId(GeneratorUUID.generateUUID());
				role.setVcUserId(ids[i]);
				role.setVcRoleId(roleId);
				roleAccountList.add(role);
			}
		}
		this.basedao.addPos(roleAccountList);
	}

	/* (non-Javadoc)
	 * @see com.csmis.sys.role.logic.IRoleLogic#changeUserRole(java.lang.String[], java.lang.String)
	 */
	public void updateUserRole(String[] roleIds, String userId)
			throws AppException {
		try{
		     this.basedao.deleteSearchHQL("from SysAccountRole po where po.vcUserId = '"+userId+"'");
		}catch(Exception e){
			e.printStackTrace();
		}
		if(roleIds!=null&&roleIds.length>0){
			for(int i=0;i<roleIds.length;i++){
				SysAccountRole role = new SysAccountRole();
				role.setVcId(GeneratorUUID.generateUUID());
				role.setVcRoleId(roleIds[i]);
				role.setVcUserId(userId);
				this.basedao.addPo(role);
			}
		}
	}

	
}
